<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>电费管理</title>
<%@include file="../static/include/head.jsp"%>

<script src="<%=request.getContextPath()%>/static/assets/global/scripts/metronic.js" type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/static/assets/admin/layout/scripts/layout.js" type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/static/assets/admin/layout/scripts/quick-sidebar.js" type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/static/assets/admin/layout/scripts/demo.js" type="text/javascript"></script>

<link href="<%=request.getContextPath()%>/static/assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
<script src="<%=request.getContextPath()%>/static/assets/global/plugins/datatables/media/js/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/static/assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js" type="text/javascript"></script>

<link href="<%=request.getContextPath()%>/static/assets/global/plugins/bootstrap-datepicker/css/datepicker.css" rel="stylesheet" type="text/css" />
<script src="<%=request.getContextPath()%>/static/assets/global/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js" type="text/javascript"></script>

<script>
	jQuery(document).ready(function() {
		Metronic.init();
		Layout.init();
		QuickSidebar.init();
		Demo.init();
		$("#serchButton").click(function() {
			tableBillInit.fnDraw();
		});
		
		$('.date-picker').datepicker({
			language : 'cn',
			todayBtn : false,
			autoclose : true,
			forceParse : true,
			format : "yyyy-mm-dd"
		});
		
		$('input[name=bill_currentTimeStr_edit]').change(function() { 
			calculate_yongdianTS();
		}); 
		
		$('input[name=bill_lastTimeStr_edit]').change(function() { 
			calculate_yongdianTS();
		}); 
		
		$('input[name=bill_stationNo_edit]').change(function() { 
			bill_stationNo_edit_onchange();
		});  
		
		$('input[name=bill_ammeterNo_edit]').change(function() { 
			bill_ammeterNo_edit_onchange();
		}); 
		
		var tableBillInit = $("#table_bill").dataTable({
				"paging" : true, //翻页功能
				"lengthChange" : true, //改变每页显示数据数量
				"searching" : false, //过滤功能
				"ordering" : true, //排序功能
				"order" : [ [ 0, "desc" ] ],
				"processing" : false,
				"serverSide" : true, //服务器数据
				"lengthMenu" : [ 10, 15, 30, 50 ],
				"pageLength" : 10,
				"language" : {
				"processing" : "努力加载中...",
				"search" : "搜索&nbsp;:",
				"lengthMenu" : "一页显示 _MENU_ 条数据",
				"info" : "当前第 _START_ 条到第 _END_ 条记录，共 _TOTAL_ 条记录",
				"loadingRecords" : "正在努力的加载数据...",
				"infoEmpty" : "当前第 0 条到第 0 条记录，共 0 条记录",
				"emptyTable" : "没有找到您查询的数据信息"
			},
			"ajax" : {
				"url" : "bill/getBillPage",
				"type" : "POST",
				"data" : function(data) {
					data.stationName = $("#stationName").val();
					data.stationNo = $("#stationNo").val();
					data.billTime = $("#billTime").val();
				}
			},
			"columns" : [{
					"data" : "id"
				},{
					"data" : "cost"
				},{
					"data" : "stationNo"
				},{
					"data" : "stationName"
				},{
					"width": "120px",
					"data" : "lastTime",
					render: function ( data, type, row ) {
						if(data!=null){
							return new Date(data).toLocaleString().replace("/","年").replace("/","月").replace(" ","日 ").split(" ")[0];
				        }else{
				        	return '';
				        }
				    }
				},{
					"width": "120px",
					"data" : "currentTime",
					render: function ( data, type, row ) {
						if(data!=null){
							return new Date(data).toLocaleString().replace("/","年").replace("/","月").replace(" ","日 ").split(" ")[0];
				        }else{
				        	return '';
				        }
				    }
				},{
					"data" : "billTime"
				},{
					"data" : "jfPerson"
				},{
					"data" : "yfPerson"
				},{
					"orderable": false,
					"data" : null,
					"defaultContent" : "<div style='width:120'><button type='button' onclick='lookBillInit($(this))' class='btn blue btn-xs'>查看 <i class='fa fa-edit'></i></button><button type='button' onclick='deleteBillInit($(this))' class='btn red btn-xs'>删除 <i class='fa fa-times'></i></button></div>"
				}]
		});
	});
	
	
	// 新增按钮初始化
/* 	function addBillInit() {
		clearInput();
		$("#edit_bill_btn").hide();
	    $("#submit_bill_btn").show();
	    $("#billInfoTable input[type='text']").each(function(e){
	    	$(this).removeAttr("readonly");
	    	$(this).removeAttr("style");
	    	$(this).css("background-color","");
		 });
	    $("#bill_lastTimeStr_edit").attr("readonly","readonly");
	    $("#bill_lastTimeStr_edit").css("background-color","white");
	    $("#bill_lastTimeStr_edit").removeAttr("disabled");
	    $("#bill_currentTimeStr_edit").attr("readonly","readonly");
	    $("#bill_currentTimeStr_edit").css("background-color","white");
	    $("#bill_currentTimeStr_edit").removeAttr("disabled");
		$("#managerBill_div").modal("show");
		$("#managerBill_title").text("新增电费条目");
		$("#managerBill_color").attr("class","portlet box green");		
    }; */
    
    function addBillInit() {
    	window.location.href = 'toEditBill';
    }
    
    
 	// 查看按钮初始化
	function lookBillInit(item) {
		clearInput();
		$("#edit_bill_btn").show();
	    $("#submit_bill_btn").hide();
	    $("#billInfoTable input[type='text']").each(function(e){
	    	$(this).attr("readonly","readonly");
	    	$(this).attr("style","border:0px");
	    	$(this).css("background-color",$(this).parents('td').css('background-color'));
		 });
	    $("#bill_lastTimeStr_edit").attr("disabled", "disabled");
	    $("#bill_currentTimeStr_edit").attr("disabled", "disabled");
		$("#managerBill_title").text("查看电费条目详情");
		$("#managerBill_color").attr("class","portlet box blue");
        var tableRow = item.parents('tr')[0];
        var idTh = $(tableRow).children().get(0);
        $.post("<%=request.getContextPath()%>/bill/getBillById", 
    		{ 
    			id: $(idTh).text()
    		}, 
    		function(json){
    			for(var index in json.data){
    				var val = json.data[index];
    				if(typeof($("#bill_"+index+"_edit")) != "undefined"){
    					$("#bill_"+index+"_edit").val(json.data[index]);
    					<!-- 下拉框处理 -->
						if(index=='ammeterBeiLv'){
							$("#bill_"+index+"_edit").val(json.data[index]);						
						}
    				}
    			}
    			
				$("#managerBill_div").modal("show");
			},"json"
    	);
    };
	
	// 修改按钮初始化
	function editBillInit() {
		$("#edit_bill_btn").hide();
	    $("#submit_bill_btn").show();
	    $("#billInfoTable input[type='text']").each(function(e){
	    	$(this).removeAttr("readonly");
	    	$(this).removeAttr("style");
	    	$(this).css("background-color","");
		 });
	    $("#bill_lastTimeStr_edit").attr("readonly","readonly");
	    $("#bill_lastTimeStr_edit").css("background-color","white");
	    $("#bill_lastTimeStr_edit").removeAttr("disabled");
	    $("#bill_currentTimeStr_edit").attr("readonly","readonly");
	    $("#bill_currentTimeStr_edit").css("background-color","white");
	    $("#bill_currentTimeStr_edit").removeAttr("disabled");
		$("#managerBill_title").text("修改电费条目资料");
		$("#managerBill_color").attr("class","portlet box yellow");
    };
    
 	// 删除按钮初始化
	function deleteBillInit(item) {
		$("#bill_id_delete").val("");
        var tableRow = item.parents('tr')[0];
        var idTh = $(tableRow).children().get(0);
        var stationNameTh = $(tableRow).children().get(4);
        var stationNewNumberTh = $(tableRow).children().get(1);
        var id = $(idTh).text();
        $("#bill_id_delete").val(id);
        var message = "您确定要删除编号:"+id+",的电费条目吗?(本操作不可恢复)"
        $("#deleteBill_p").text(message);
        $("#deleteBill_div").modal("show");
    };
	
	
	//简单校验用户输入
	function submit_validation(){
		var obj = new Object();
		$("#billInfoTable input[type='text']").each(function(e){
			var stationId = $(this).attr("id");
		    var idArray = stationId.split('_');
		    obj[idArray[1]] = $(this).val();
		 });
		var id = $("#bill_id_edit").val();
		
		$("#billInfoTable select").each(function(e){
		    var inputId = $(this).attr("id");
		    var idArray = inputId.split('_');
		    if($(this).val()!=''){
			    obj[idArray[1]] = $(this).val();
		    }
		});
		
		if(id == ''){
			save_bill(obj)
		}else{
			update_bill(obj)
		}
		clearInput();
	}
	
	// 更新基站提交
	function update_bill(obj){
		$.post("<%=request.getContextPath()%>/bill/updateBill",obj, 
	    	function(result){
	    		if(result == -1){
					alert("系统内部错误,请与管理员联系！");
					$("#managerBill_div").modal("hide");
					$("#table_bill").dataTable().fnDraw();
				}else if(result == 0){
					alert("修改电费条目成功！");
					$("#managerBill_div").modal("hide");
					$("#table_bill").dataTable().fnDraw();
				}
			}
	    );
	}
	
	// 增加电费提交
	function save_bill(obj){
		$.ajax({
			type:"POST",
			url:"<%=request.getContextPath()%>/bill/add",
			data:obj,
			dataType :'json',
			timeout:"20000",
			success:function(data,textStatus){
				var msg = data.msg;
				var code = data.code;
				if(code == 0 && 'succ' == msg){
					alert("保存成功");
					$("#managerBill_div").modal("hide");
					$("#table_bill").dataTable().fnDraw();
				}				
			},
			error : function(XMLResponse) {
				alert("提交超时请检查网络环境");
				$("#managerBill_div").modal("hide");
				$("#table_bill").dataTable().fnDraw();
			}
		});
	}
	
	function bill_ammeterNo_edit_onchange(){
		var obj = new Object();
		$("#billInfoTable input[type='text']").each(function(e){
			var stationId = $(this).attr("id");
		    var idArray = stationId.split('_');
		    obj[idArray[1]] = $(this).val();
		 });
		$.ajax({
			type:"POST",
			url:"<%=request.getContextPath()%>/bill/checkAmmeterNo",
			data:obj,
			dataType :'json',
			timeout:"20000",
			success:function(data,textStatus){
				var msg = data.msg;
				var code = data.code;		
				if(code == 0 && 'succ' == msg){
					$("#bill_lastDegrees_edit").val(data.currentDegrees);
					var time = new Date(data.currentTime).toLocaleString().replace("/","-").replace("/","-").replace(" "," ").split(" ")[0]; 
					$("#bill_lastTimeStr_edit").val(time);
					calculate_yongdianTS();
				}else{
					alert(msg);
				}				
			},
			error : function(XMLResponse) {
				alert("提交超时请检查网络环境");
				$("#managerBill_div").modal("hide");
				$("#table_bill").dataTable().fnDraw();
			}
		});
	}
	
	function bill_stationNo_edit_onchange(){
		var obj = new Object();
		$("#billInfoTable input[type='text']").each(function(e){
			var stationId = $(this).attr("id");
		    var idArray = stationId.split('_');
		    obj[idArray[1]] = $(this).val();
		 });
		$.ajax({
			type:"POST",
			url:"<%=request.getContextPath()%>/bill/checkStationNo",
			data:obj,
			dataType :'json',
			timeout:"20000",
			success:function(data,textStatus){
				var msg = data.msg;
				var code = data.code;
				if(code == 0 && 'succ' == msg){
					$("#bill_stationName_edit").val(data.name);
				}else{
					alert(msg);
				}				
			},
			error : function(XMLResponse) {
				alert("提交超时请检查网络环境");
				$("#managerBill_div").modal("hide");
				$("#table_bill").dataTable().fnDraw();
			}
		});
	}
	
	// 删除基站提交
	function delete_bill(){
		$.post("<%=request.getContextPath()%>/bill/deleteBillById", 
	    	{ 
				id:$("#bill_id_delete").val()
	    	}, 
	    	function(result){
	    		if(result == -1){
					alert("系统内部错误,请与管理员联系！");
					$("#deleteBill_div").modal("hide");
					$("#bill_id_delete").val("");
					$("#table_bill").dataTable().fnDraw();
				}else if(result == 0){
					alert("删除电费条目成功！");
					$("#deleteBill_div").modal("hide");
					$("#bill_id_delete").val("");
					$("#table_bill").dataTable().fnDraw();
				}
			}
	    );
	}
	
	function clearInput(){
		$("#billInfoTable input[type='text']").each(function(e){
			$(this).val("");
		 });
	}
	
	
	function download() {
		var snav = $("#stationName").val();
		var snov = $("#stationNo").val();
		var billTime = $("#billTime").val();
		window.location.href = "<%=request.getContextPath()%>/bill/exportExcel?stationName="+snav+"&stationNo="+snov+"&billTime="+billTime;
	}
	
	function toUpload(){
		window.location.href = "<%=request.getContextPath()%>/bill/toUploadBill";
	}
	
	function calculate_yongdianTS(){
		var lastTime_str = $("#bill_lastTimeStr_edit").val();
		var currentTime_str = $("#bill_currentTimeStr_edit").val();
		if(lastTime_str!=''&&currentTime_str!=''){
			//根据日期字符串转换成日期
		    var regEx = new RegExp("\\-","gi");
		    var dependedVal=lastTime_str.replace(regEx,"/");
		    var milliseconds=Date.parse(dependedVal);
		    var lastTime=new Date();
		    lastTime.setTime(milliseconds);
		    
		    dependedVal=currentTime_str.replace(regEx,"/");
		    milliseconds=Date.parse(dependedVal);
		    var currentTime=new Date();
		    currentTime.setTime(milliseconds);
		    
		    var date3=currentTime.getTime()-lastTime.getTime();  //时间差的毫秒数
		 	 //计算出相差天数
		  	var days=Math.floor(date3/(24*3600*1000));
		  	$("#bill_useDays_edit").val(days);
		}
	}
</script>
</head>
<body class="page-header-fixed page-quick-sidebar-over-content">
	<%@include file="../static/include/header.jsp"%>
	<div class="clearfix"></div>
	<div class="page-container">
		<%@include file="../static/include/sidebar.jsp"%>
		<!-- BEGIN CONTENT -->
		<div class="page-content-wrapper">
			<div class="page-content">
				<!-- BEGIN STYLE CUSTOMIZER -->
				<%@include file="../static/include/styles.jsp"%>
				<!-- END STYLE CUSTOMIZER -->
				<div class="row">
					<div class="col-md-12">
						<!-- BEGIN PAGE HEADER-->
						<h3 class="page-title">
							电费管理 <small>电费条目一览</small>
						</h3>
						<div class="page-bar">
							<ul class="page-breadcrumb">
								<li><i class="fa fa-home"></i> <a href="index">主页</a> <i class="fa fa-angle-right"></i></li>
								<li><a href="billManager">电费管理</a></li>
							</ul>
						</div>
						<!-- END PAGE HEADER-->
						<!-- BEGIN ROW -->
						<div class="portlet box blue-madison">
							<div class="portlet-title">
								<div class="caption">
									<i class="fa fa-user"></i>电费条目
								</div>
								<div class="tools">
									<a href="javascript:;" class="collapse"></a>
								</div>
							</div>
							<div class="portlet-body">
								<div>
									<ul class="nav nav-tabs">
										<li style="float: left">
											<button type="button" onclick="addBillInit()" class="btn green">
												新增 <i class="fa fa-plus"></i>
											</button>
										</li>
										<li style="float: left">
											&nbsp;
											<button type="button" class="btn green" onclick="toUpload()">
												上传 <i class="fa fa-plus"></i> 
											</button>
										</li> 
										<li style="float: left">&nbsp;
											<button class="btn yellow" onclick="download()">
												下载 <i class="fa fa-plus"></i>
											</button>
										</li>
										<li style="float: right; margin-left: 5px">
											<button id="serchButton" type="button" class="btn green">搜索</button>
										</li>
										<li style="float: right; margin-left: 5px"><input id="billTime" type="text" class="form-control" placeholder="请输入次数"></li>								
										<li style="float: right; margin-left: 5px"><input id="stationNo" type="text" class="form-control" placeholder="请输入基站站号"></li>
										<li style="float: right"><input id="stationName" type="text" class="form-control" placeholder="请输入基站名称"></li>
									</ul>
									<div class="tab-pane fade active in" id="tab_1_1">
										<table id="table_bill" class="table table-striped table-hover table-bordered">
											<thead>
												<tr>
													<th>编号</th>
													<th>金额</th>
													<th>站号</th>
													<th>基站名称</th>
													<th>上期日期</th>
													<th>本次日期</th>
													<th>电费次数</th>
													<th>甲方经手人</th>
													<th>乙方经手人</th>
													<th>操作</th>
												</tr>
											</thead>
											<tbody>
											</tbody>
										</table>
									</div>
								</div>
							</div>
						</div>
						<!-- END ROW -->
					</div>
				</div>
			</div>
			<!-- END CONTENT -->
		</div>
	</div>
	<%@include file="../static/include/footer.jsp"%>
	<!-- modal BEGIN -->
	<div id="managerBill_div" class="modal fade" tabindex="-1" aria-hidden="true">
		<div class="modal-dialog">
			<div id="managerBill_color" class="portlet box blue" style="width: 900px">
				<div class="portlet-title">
					<div class="caption">
						<i class="fa fa-user"></i>电费管理
					</div>
					<div id="managerBill_title" class="tools"></div>
				</div>
				<div class="portlet-body form">
					<div class="form-body">
						<table style="margin: 3px" class="table table-striped table-hover table-bordered" id="billInfoTable">
							<tr hidden="true">
								<td>编号</td>
								<td><input class="col-md-12" id="bill_id_edit" type="text" readonly="readonly"></input></td>
								<td></td>
								<td></td>
							</tr>
							<tr>
								<td>合计金额</td>
								<td><input class="col-md-12" id="bill_cost_edit" type="text" onkeyup="value=value.replace(/[^\d.]/g,'')"></input></td>
								<td>基站站号</td>
								<td><input class="col-md-12" id="bill_stationNo_edit" name="bill_stationNo_edit" type="text"></input></td>
							</tr>
							<tr>
								<td>上期度数</td>
								<td><input class="col-md-12" id="bill_lastDegrees_edit" type="text" onkeyup="value=value.replace(/[^\d]/g,'')"></input></td>
								<td>本次度数</td>
								<td><input class="col-md-12" id="bill_currentDegrees_edit" type="text" onkeyup="value=value.replace(/[^\d]/g,'')"></input></td>
							</tr>
							<tr>
								<td>实际度数</td>
								<td><input class="col-md-12" id="bill_degrees_edit" type="text" onkeyup="value=value.replace(/[^\d]/g,'')"></input></td>
								<td>上期日期</td>
								<td><input id="bill_lastTimeStr_edit" name="bill_lastTimeStr_edit" class="input-medium date-picker" type="text" /> <!-- <input class="col-md-12" id="bill_lastTime_edit" type="text"></input> --></td>
							</tr>
							<tr>
								<td>本次日期</td>
								<td><input id="bill_currentTimeStr_edit" name="bill_currentTimeStr_edit" class="input-medium date-picker" type="text" /> <!-- <input class="col-md-12" id="bill_currentTime_edit" type="text"></input> --></td>
								<td>用电天数</td>
								<td><input class="col-md-12" id="bill_useDays_edit" type="text" onkeyup="value=value.replace(/[^\d]/g,'')"></input></td>
							</tr>						
							<tr>
								<td>物业单价</td>
								<td><input class="col-md-12" id="bill_price_edit" type="text" onkeyup="value=value.replace(/[^\d.]/g,'')"></input></td>
								<td>移动单价</td>
								<td><input class="col-md-12" id="bill_quotePrice_edit" type="text" onkeyup="value=value.replace(/[^\d.]/g,'')"></input></td>
							</tr>
							<tr>
								<td>电费次数</td>
								<td><input class="col-md-12" id="bill_billTime_edit" type="text"></input></td>
								<td>基站名称</td>
								<td><input class="col-md-12" id="bill_stationName_edit" type="text"></input></td>
							</tr>
							<tr>
								<td>甲方经手人</td>
								<td><input class="col-md-12" id="bill_jfPerson_edit" type="text"></input></td>
								<td>乙方经手人</td>
								<td><input class="col-md-12" id="bill_yfPerson_edit" type="text"></input></td>		
							</tr>
							<tr>								
								<td>支出数</td>
								<td><input class="col-md-12" id="bill_outTradeNo_edit" type="text"></input></td>
								<td>发票类型</td>
								<td><input class="col-md-12" id="bill_faPiaoType_edit" type="text"></input></td>
							</tr>
							<tr>								
								<td>电表类型</td>
								<td>
									<select class="form-control" id="bill_ammeterType_edit">
										<option value="" selected="selected">请选择</option>  
										<option value="机房表">机房表</option>
										<option value="配电室表">配电室表</option>
										<option value="卡表">卡表</option>
										<option value="供电局"> 供电局</option>
									</select>
								</td>
								<td>电表倍率</td>
								<td>
									<select class="form-control" id="bill_ammeterBeiLv_edit">
										<option value="" selected="selected">请选择</option>
										<option value="1">直读（1倍）</option>
										<option value="10">10倍</option>
										<option value="15">15倍</option>
										<option value="20">20倍</option>
										<option value="30">30倍</option>
										<option value="50">50倍</option>
									</select>
								</td>
							</tr>
							<tr>
								<td>备注</td>
								<td colspan="3"><input class="col-md-12" id="bill_description_edit" type="text"></input></td>
							</tr>
						</table>
					</div>
					<div class="form-actions right">
						<button type="button" data-dismiss="modal" class="btn default">返回</button>
						<button id="edit_bill_btn" type="button" onclick="editBillInit()" class="btn yellow" hidden="true">修改</button>
						<button id="submit_bill_btn" type="button" onclick="submit_validation()" class="btn green">提交</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- modal END -->

	<!-- modal BEGIN -->
	<div id="deleteBill_div" class="modal fade" tabindex="-1" aria-hidden="true">
		<div class="modal-dialog">
			<div class="portlet box red">
				<div class="portlet-title">
					<div class="caption">
						<i class="fa fa-user"></i>电费管理
					</div>
					<div class="tools">删除电费条目</div>
				</div>
				<div class="portlet-body form">
					<div class="form-body">
						<input class="col-md-10" id="bill_id_delete" type="text" readonly="readonly" hidden="true"></input>
						<p id="deleteBill_p"></p>
					</div>
					<div class="form-actions right">
						<button type="button" data-dismiss="modal" class="btn default">返回</button>
						<button type="button" onclick="delete_bill()" class="btn red">删除</button>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- modal END -->
</body>
</html>
